package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowAddForm;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowEditForm;
import com.yikes.srb.leadingModle.form.transFlow.TransFlowQuery;
import com.yikes.srb.leadingModle.vo.TransFlowVO;
import com.yikes.srb.service.TransFlowService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 交易流水表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "TransFlowController", description = "交易流水表")
@RestController
@RequestMapping("/admin/trans_flow")
public class TransFlowController {

    private final TransFlowService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<TransFlowVO>> listPage(TransFlowQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<TransFlowVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody TransFlowAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody TransFlowEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "获取列表")
    @GetMapping("/list")
    public Result list(HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        List<TransFlowVO> list = service.selectByUserId(userId);
        return Result.ok(list);
    }

}
